<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%-- <%@ include file="/common/commonJs.jsp"%> --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript"
	src="${ctx}/js/comm.js"></script>
<script type="text/javascript"
	src="${ctx}/js/jquery-1.8.0.min.js"></script>
	<script type="text/javascript"
	src="./enum.js"></script>
<script>
	var ctx = '{ctx}';
	var tableColumnXslDoc = commLoad("${ctx}/createTablestruts/createTableRow.xsl");
	
	//添一表列数据
	function addColumn(obj) {
		var emptyStrutXml = "<column name='' dataType='2' isPk=''></column>";
 		var tbody = obj.parentNode.parentNode.parentNode;
		$(tbody).append(transform(emptyStrutXml,tableColumnXslDoc));
	}
	
	$(function(){
		
		$("#entityContentTool").hide();
		$("#entityOut").hide();
		
		//点产生
		$("input[name='create']").click(function(){
			var entityName = $("[name='entity'] [name='name']").val();
			var properties = $("[name='entity'] [name='property']");
			var dataType = $("[name='entity'] [name='dataType']");
			
// 			
			var classXml = '<class extends="super"    name="'+entityName + 
							'"   implements="abc" package="com.catt" isAbstract="0" isFinal="0" visitControl="1" >';
			var propertiesXml = '<properties>';
			
			for(var i = 0; i< properties.length ;i++ ){
				propertiesXml += '<property visitControl="1" isStatic="0" isFinal="0" name="' + $(properties[i]).val() +
							  '"  type="' + PL.Java.type[$(dataType[i]).val()] + '" ></property>';
			}
			propertiesXml += '</properties>';
			classXml += propertiesXml + "</class>";
// 			alert(classXml);
			$("#sqlOut").hide();
			$("#entityOut").val(classXml).show();
		});
		
		
		//点保存
		$("input[name='save']").click(function(){
			var entityXml = $("#entityOut").val();
			var classTransformor = commLoad("${ctx}/createTablestruts/entityTransformor.xsl");
			var classDefined = changeXmlDoc2Str(commLoad("${ctx}/createTablestruts/operates.xml"));
			
			var classStr = transform(entityXml,classTransformor);
			$("#entityClass").val(classStr);
			//alert(classStr);
		});
		
	});
	
	
	//转换文档to html
	function transform(emptyStrutXml,tableColumnXslDoc){
		var resultDocument = null;
		var rowXmlDoc = commLoadXML(emptyStrutXml);
		try {
			if (document.implementation && document.implementation.createDocument) {//webkit
				xsltProcessor = new XSLTProcessor();
				//引入样式表
				xsltProcessor.importStylesheet(tableColumnXslDoc);
				//转换文档
				var resultDoc = xsltProcessor.transformToDocument(rowXmlDoc, document);
				var xmls=new XMLSerializer();
			    resultDocument = xmls.serializeToString(resultDoc);
			}
		} catch (e) {//ie
			//转换文档
			resultDocument = rowXmlDoc.transformNode(tableColumnXslDoc);
		}
		return resultDocument;
	}

	function callback(args, datasXmlDoc) {
		var showStr = null;
		var datasXslDoc = commLoad("${ctx}/createEntity/createEntityUI.xsl");
		
		var xslStr =  changeXmlDoc2Str(datasXslDoc);
		try {
			  showStr=datasXmlDoc.transformNode(datasXslDoc);
			  document.getElementById("entityContent").innerHTML=showStr;
			  alert(showStr);
			  }
		catch(e){
			showStr = (new XMLSerializer).serializeToString(xml2New(datasXmlDoc,datasXslDoc));
			var divContent = document.getElementById("entityContent");
			divContent.innerHTML = showStr;
		  }
		alert(showStr);
		
		$("#entityContentTool").show();
		//alert($($("#id.dataType").val()+"#name.dataType").val());
	}
	
	//删除一行
	function removeColumn(obj) {
		//alert(1);
		var tbody = obj.parentNode.parentNode.parentNode;
		var tbody = obj.parentNode.parentNode.parentNode;
		var row = obj.parentNode.parentNode;
		if(row.rowIndex == 1 || row.rowIndex == 2){
			alert("表至少有一个属性！");
			return;
		}
		tbody.removeChild(row);
	}

	/**
	*
	* 收集数据
	*
	**/
	function pickUpData() {
		var table = $("#table").val();
		var tableName = $("#tableName").val();
		var pks = $("input[name='pk']");
		var columnNames = $("input[name='columnName']");
		var dataTypes = $("*[name='dataType']");
		var tableStrutStr = " create table " + tableName + " ( ";
		for ( var i = 0; i < columnNames.length; i++) {
			tableStrutStr += $(columnNames[i]).val() + " " + DB.MySQL.dataType[$(dataTypes[i]).val()];
			if(pks[i].checked){
				tableStrutStr += " primary key ";
			}
			
			if (i + 1 < columnNames.length)
				tableStrutStr += ",";
		}
		tableStrutStr += " ) ";

		console.log(tableStrutStr);
		
		return tableStrutStr;
	}

	/**
	 ** 查检主键支持
	 */
	function checkPkNumSupported() {
		var isSupportedParmaryKeyNum = true;
		if ($("input[name='pk']:checked").length > 2) {
			isSupportedParmaryKeyNum = false;
			alert("程序暂时支持最多2主键!");
		}
		return isSupportedParmaryKeyNum;
	}

	/**
	**
	*  保存表结构
	*/
	function save() {
		if (!checkPkNumSupported())
			return;
		$("#sqlOut").val(pickUpData());
		var param = {content:$("#sqlOut").val()};
		
		$.ajax({
				data : ctx + '',
				url : param ,
				dataType:'json',
				success:function(backData){
					
				}
			});
	}

	function createTableStructXml() {
		var table = document.getElementById("table");
		var tableName = document.getElementById("tableName");
		var pks = document.getElementsByName("pk");
		var columnNames = document.getElementsByName("columnName");

		var dataTypes = document.getElementsByName("dataType");
		var tableStructXml = "<table name='"+tableName.value+"'> ";
		for ( var i = 0; i <= columnNames.length - 1; i++) {
			tableStructXml = tableStructXml + "<column name = '"
					+ columnNames[i].value + "' " + "dataType = '"
					+ dataTypes[i].value + "' " + "isPk = ";
			if (pks[i].checked == true)
				tableStructXml = tableStructXml + "'" + true + "' " + " />";
			else
				tableStructXml = tableStructXml + "'" + false + "' " + " />";
		}
		tableStructXml = tableStructXml + " </table>";
		//alert(tableStructXml);
		return tableStructXml;
	}

	function rebackXmlDoc(xmlStr) {
		var oParser = new DOMParser();
		var retDom = oParser.parseFromString(xmlStr, "text/xml");
		return retDom;
	}

	/**
	 * xml to string
	 * 
	 */
	function changeXmlDoc2Str(doc) {
		var xml = null;
		try{
			if (document.implementation && document.implementation.createDocument)
				xml = (new XMLSerializer()).serializeToString(doc);
		}catch (e) {
			// TODO: handle exception
			xml = doc.xml;
		}
		return xml;
	}

	function sendTableStructXml() {
		var xmlStr = createTableStructXml();
		var xmlDoc = rebackXmlDoc(xmlStr);
		var xml = changeXmlDoc2Str(xmlDoc);
// 		alert(xml);
		var url = "${ctx}/dataCreate.action?index=1";
	 	commHttpSend(url, xml, true, "callback", null);
		
	 	
/* 		$.ajax({
			data : xml,
			url : url,
			dataType : 'xml',
			type : "POST",
			success : function(backData) {
				callback();
			}
		});
 */	}

	function createTableStrutXml() {
		var tableCreateSql = pickUpData();

	}
	
	
	
</script>
<title>Insert title here</title>
</head>
<body>
	<table border="1" id="table">
		<tbody>
			<tr>
				<td align="center">表名</td>
				<td colspan="4"><input type="text" id="tableName" value="student"></input>
				</td>
			</tr>
			<tr>
				<td align="center">主键</td>
				<td align="center">列名</td>
				<td><input type="button" value="+" onclick="addColumn(this);"></input>
				</td>
				<td><input type="button" value="-"
					onclick="removeColumn(this);"></input>
				</td>
				<td align="center">数据类型</td>
			</tr>
			<tr>
				<td><input type="checkbox" id="pk" name="pk"></input>
				</td>
				<td><input type="text" style="width: 100%" id="columnName" name="columnName" value="name"></input>
				</td>
				<td><input type="button" value="+" onclick="addColumn(this);"></input>
				</td>
				<td><input type="button" value="-"
					onclick="removeColumn(this);"></input>
				</td>
				<td><input type="text" style="width: 100%" id="dataType" name="dataType" value="2"></input>
				</td>
			</tr>
		</tbody>
	</table>
	<div id="tableContent">
		<table>
			<tr>
				<td><input type="button" onclick="pickUpData()" value="确定"></input>
				</td>
				<td><input type="button" onclick="save()" value="生成"></input>
				</td>
				<td><input type="button" onclick="createTableStrutXml()"
					value="保存"></input>
				</td>
				<td><input type="button" onclick="sendTableStructXml()"
					value="发送"></input>
				</td>
			</tr>
		</table>
	</div>
	<div id="entityContent"></div>
	
	<div id="entityContentTool">
		<table>
			<tr>
				<td><input id="ensure"   name="ensure" type="button"  value="确定"></input>
				</td>
				<td><input id="create"   name="create" type="button"  value="生成"></input>
				</td>
				<td><input id="save"   name="save" type="button" value="保存"></input>
				</td>
				<td><input id="send"   name="send" type="button" value="发送"></input>
				</td>
			</tr>
		</table>
	</div>
	<div>
		<textarea id="sqlOut" style="width:424px;height: 300px;"></textarea>
	</div>
	
	<div>
		<textarea id="entityOut" style="width:424px;height: 300px;"></textarea>
	</div>
	
	<div>
		<textarea id="entityClass" style="width:424px;height: 300px;"></textarea>
	</div>
</body>
</html>